package top.suven.base.util.createcode;

import com.alibaba.fastjson.JSON;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**   
 * @Title: CreateObject.java 
 * @author Joven.wang   
 * @date   2017年2月21日
 * @version V1.0  
 * @Description: TODO(说明)  
 */

public class CreateObject {
	
	public static void main(String[] args) {
		Class clazz = null;//RespAddressList.class;
		createResult(clazz);
	}
	
	public static void main2(String[] args) throws SecurityException, NoSuchFieldException {
		Class clazz = null;//RespAddressList.class;
		Field field = clazz.getField("List");
 
        Type genericFieldType = field.getGenericType();
             
        if(genericFieldType instanceof ParameterizedType){
            ParameterizedType aType = (ParameterizedType) genericFieldType;
            Type[] fieldArgTypes = aType.getActualTypeArguments();
            for(Type fieldArgType : fieldArgTypes){
                Class fieldArgClass = (Class) fieldArgType;
                System.out.println("fieldArgClass = " + fieldArgClass);
            }
        }
    }
	
	public static Class getclaxx(Field field){
		if(field == null){
			return null;
		}
		 Type genericFieldType = field.getGenericType();
		 if(genericFieldType != null){
			 if(genericFieldType instanceof ParameterizedType){
		            ParameterizedType aType = (ParameterizedType) genericFieldType;
		            Type[] fieldArgTypes = aType.getActualTypeArguments();
		            for(Type fieldArgType : fieldArgTypes){
		                Class fieldArgClass = (Class) fieldArgType;
		                System.out.println("fieldArgClass = " + fieldArgClass);
		                return fieldArgClass;
		            }
		        }
		 }
          return null;
	}

	public static Map<String,Object> createResult(Class clazz) {
		Map<String,Object> map = new HashMap<>();
		map.put("baseResponse", "\t");
		map.put("baseClass", " \t");
		map.put("body", "");
		createClassResult(clazz, map);
		return map;
	}

	public static void createClassResult(Class clazz,Map<String,Object> map) {
		
		String baseparam = "#### 响应参数类型说明 :\n";
		String temp = "  %s|  %s|  %s";
		StringBuffer headsb = new StringBuffer(); // sql 条件
		String head = String.format(temp, CreateCodeDoc.formatStr("参数", 19), CreateCodeDoc.formatStr("类型", 9),"说明");
		headsb.append(head).append("\n");
		head = String.format(temp, CreateCodeDoc.formatStr("----", 20), CreateCodeDoc.formatStr("----", 10),"----");
		headsb.append(head).append("\n");
		String headResult = headsb.toString();
	
		
		try {
			Object obj = ((Class<?>)clazz).newInstance();
			Field[] rfd = ((Class<?>)clazz).getDeclaredFields();
			
			StringBuffer response = new StringBuffer(); // sql 条件
			Field objectField   = null;
			boolean isList   = false;
			for(Field pr : rfd ){
				try {
					DocDesc meta = pr.getAnnotation(DocDesc.class);
					String desc = meta != null ? meta.desc() : "";
					String typeName = CreateCodeDoc.subString(pr.getType().getTypeName()); 
					String rspresult = String.format(temp, CreateCodeDoc.formatStr(pr.getName(), 20),CreateCodeDoc.formatStr(typeName, 10),desc);
					response.append(rspresult).append("\n");
					//对象付值；
					if (!pr.isAccessible()) {
						pr.setAccessible(true);
					}
					if("String".equals(typeName)){
						pr.set(obj,pr.getName());
					}if("List".equals(typeName)){
						objectField = pr;
						isList = true;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			System.out.println(headResult +response.toString());
			if(isList){
				map.put("baseResponse", baseparam + headResult+ response.toString());
			}else{
				map.put("response", headResult +response.toString());
			}
			Class cxx = getclaxx(objectField);
			if(null != cxx){
				map.put("baseClass", cxx.getSimpleName());
				Field[] cxxfield = ((Class<?>)cxx).getDeclaredFields();
				Object obj2 = ((Class<?>)cxx).newInstance();
				
				response = new StringBuffer(); // sql 条件
				for(Field pr : cxxfield ){
					try {
						String typeName = CreateCodeDoc.subString(pr.getType().getTypeName());
						DocDesc meta = pr.getAnnotation(DocDesc.class);
						String rspresult = String.format(temp, CreateCodeDoc.formatStr(pr.getName(), 20),CreateCodeDoc.formatStr(typeName, 10),meta.desc());
						response.append(rspresult).append("\n");
						//对象付值；
						if (!pr.isAccessible()) {
							pr.setAccessible(true);
						}
						if("String".equals(typeName)){
							pr.set(obj2,pr.getName());
						}if("List".equals(typeName)){
							objectField = pr;
							isList = true;
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				List list = new ArrayList();
				list.add(obj2);
				objectField.set(obj,list);
				System.out.println(headResult +response.toString());
				map.put("response", headResult +response.toString());
			}
			String body = JSON.toJSONString(obj);
			body = body.substring(1,body.length()-1);
			map.put("body", body);
			System.out.println(map.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
//		return map;
		
	}
}
